package com.pengzy.comment.mybatis;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.pengzy.comment.mybatis.base.BaseEntity;
import com.pengzy.comment.mybatis.base.IBaseMapper;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author xiaozhi 2023/7/7
 */

public class CodeGenerator {

    // 处理 all 情况
    protected static List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }

    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir")+"/xz_comment/comment-mybatis";
        FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/xz_cloud?autoReconnect=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2B8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true",
                "root", "0000")
                .globalConfig(builder -> {
                    builder.author("xiaozhi") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .fileOverride() // 覆盖已生成文件
                            .outputDir(projectPath + "/src/main/java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("com.pengzy.comment") // 设置父包名
                            .moduleName("mybatis") // 设置父包模块名
                            .pathInfo(Collections.singletonMap(OutputFile.xml, projectPath+"/src/main/resources/mapper/")); // 设置mapperXml生成路径
                })
                .strategyConfig((scanner, builder) -> builder
                        .addInclude(getTables("sys_log"))  //请输入表名，多个英文逗号分隔？所有输入 all
//                        .addInclude(getTables("hk_device_alarm"))  //请输入表名，多个英文逗号分隔？所有输入 all
                        .enableSkipView()
                        .controllerBuilder().formatFileName("")//为空表示不生产
                        .serviceBuilder().formatServiceFileName("").formatServiceImplFileName("") //为空表示不生产
                        .mapperBuilder().fileOverride().formatMapperFileName("%sMapper").formatXmlFileName("%sMapper").superClass(IBaseMapper.class)
                        .entityBuilder().enableLombok().addTableFills(
                                new Column("create_time", FieldFill.INSERT),
                                new Column("create_by_id", FieldFill.INSERT),
                                new Column("create_by", FieldFill.INSERT),
                                new Column("update_time", FieldFill.INSERT_UPDATE),
                                new Column("update_by_id", FieldFill.INSERT_UPDATE),
                                new Column("update_by", FieldFill.INSERT_UPDATE)
                        ).fileOverride().superClass(BaseEntity.class)
                        .build())
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

}
